from selenium import webdriver
from time import sleep
from PIL import Image
# from .api import main
from selenium.common.exceptions import NoSuchElementException
from .login import login_test
from .verification import main


def open_browser(name, url):
    driver = None
    if name == "chrome":
        driver = webdriver.Chrome()
    if name == "ff":
        driver = webdriver.Firefox()
    if name == "ie":
        driver = webdriver.Ie()
    driver.maximize_window()
    driver.implicitly_wait(10)
    driver.get(url)
    return driver


class TestKeys(object):

    def __init__(self, name, url, new_password):
        self.driver = open_browser(name, url)
        self.new_password = new_password

    def webui1(self, username):
        # 登录
        driver = login_test(username, self.driver, self.new_password)
        # 系统管理
        driver.find_element_by_xpath('//*[@id="app"]/section/section/aside/div/ul/div/li[6]').click()
        # 系统配置
        driver.find_element_by_xpath('//*[@id="app"]/section/section/aside/div/ul/div/li[6]/ul/div/li[7]').click()
        sleep(1)
        # 登录配置信息
        # 编辑
        driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[1]/main/form/div/div[3]/a').click()
        sleep(1)
        # 登录失败次数
        # 加+
        driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[1]/main/form/div/div[1]/div/div/div/span[2]').click()
        sleep(1)
        driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[1]/main/form/div/div[1]/div/div/div/span[2]').click()
        sleep(1)
        driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[1]/main/form/div/div[1]/div/div/div/span[2]').click()
        sleep(1)
        # 减-
        driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[1]/main/form/div/div[1]/div/div/div/span[1]').click()
        sleep(1)
        driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[1]/main/form/div/div[1]/div/div/div/span[1]').click()
        sleep(1)
        # 超过失败次数锁定时间
        # 加+
        driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[1]/main/form/div/div[2]/div/div/div/span[2]').click()
        sleep(1)
        driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[1]/main/form/div/div[2]/div/div/div/span[2]').click()
        sleep(1)
        driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[1]/main/form/div/div[2]/div/div/div/span[2]').click()
        sleep(1)
        # 减-
        driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[1]/main/form/div/div[2]/div/div/div/span[1]').click()
        sleep(1)
        driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[1]/main/form/div/div[2]/div/div/div/span[1]').click()
        sleep(1)
        # 确认
        driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[1]/main/form/div/div[3]/a').click()
        sleep(1)

        # 登录超时配置
        # 编辑
        driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[2]/main/form/div/div[2]/a').click()
        sleep(1)
        # 登录超时时间
        # 加+
        driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[2]/main/form/div/div[1]/div/div/div/span[2]').click()
        sleep(1)
        driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[2]/main/form/div/div[1]/div/div/div/span[2]').click()
        sleep(1)
        driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[2]/main/form/div/div[1]/div/div/div/span[2]').click()
        sleep(1)
        # 减-
        driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[2]/main/form/div/div[1]/div/div/div/span[1]').click()
        sleep(1)
        driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[2]/main/form/div/div[1]/div/div/div/span[1]').click()
        sleep(1)
        # 确认
        driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[2]/main/form/div/div[2]/a')
        sleep(1)

        # 任务并发数配置
        # 编辑
        driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[3]/main/form/div/div[2]/a').click()
        sleep(1)
        # 任务执行并发量
        # 加+
        driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[3]/main/form/div/div[1]/div/div/div/span[2]').click()
        sleep(1)
        driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[3]/main/form/div/div[1]/div/div/div/span[2]').click()
        sleep(1)
        driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[3]/main/form/div/div[1]/div/div/div/span[2]').click()
        sleep(1)
        # 减-
        driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[3]/main/form/div/div[1]/div/div/div/span[1]').click()
        sleep(1)
        driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[3]/main/form/div/div[1]/div/div/div/span[1]').click()
        sleep(1)
        # 确认
        driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[3]/main/form/div/div[2]/a').click()
        sleep(1)

        # 审计记录告警阈值配置
        # 编辑
        driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[4]/main/form/div/div[2]/a').click()
        sleep(1)
        # 审计记录告警阈值
        # 加+
        driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[4]/main/form/div/div[1]/div/div/div/span[2]').click()
        sleep(1)
        driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[4]/main/form/div/div[1]/div/div/div/span[2]').click()
        sleep(1)
        driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[4]/main/form/div/div[1]/div/div/div/span[2]').click()
        sleep(1)
        # 减-
        driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[4]/main/form/div/div[1]/div/div/div/span[1]').click()
        sleep(1)
        driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[4]/main/form/div/div[1]/div/div/div/span[1]').click()
        sleep(1)
        # 确认
        driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[4]/main/form/div/div[2]/a').click()
        sleep(3)

        return "执行成功"

    def quit(self):
        """
        浏览器退出
        :return:
        """
        self.driver.quit()
